package com.controller.front_controller;


import com.mypg.Info;
import com.service.InfoService;
import com.service.UInfoReadService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/front_info")
@Controller
public class FrontInfoController {

    @Autowired
    InfoService infoService;

    @Autowired
    UserService userService;

    @Autowired
    UInfoReadService uInfoReadService;

    @RequestMapping("/info_change_index.do")
    @ResponseBody
    public List<Info> doInfo_change_index(String info_class,String other_class){
        List<Info> infoList=infoService.selectInfoByInfo_class(info_class,other_class);
        return infoList;
    }

    @RequestMapping("/info_init.do")
    @ResponseBody
    public Info doInfo_init(int info_id){
        return infoService.selectInfoByinfo_id(info_id);
    }

    @RequestMapping("/info_relative.do")
    @ResponseBody
    public List<Info> doInfo_relative(String info_class,int info_id){
        List<Info> infoList=infoService.selectInfoByInfo_class(info_class,"");
        for(int i=0;i<infoList.size();i++){
            if(infoList.get(i).getInfo_id()==info_id){
                infoList.remove(i);
                break;
            }
        }
        return infoList;
    }

    @RequestMapping("/info_read_init.do")
    @ResponseBody
    public void doInfo_read_init(int info_id, HttpSession session){
        try {
            String user_name = (String) session.getAttribute("user_name");
            int user_id=userService.selectUserByUser_name(user_name).getUser_id();
            int num=uInfoReadService.selectCountByUser_idAndInfo_id(user_id,info_id);
            if(num==0){
                uInfoReadService.insertUInfoRead(user_id,info_id);
                infoService.updateInfo_readByInfo_id(info_id);
            }
        }catch (Exception e){

        }
    }
}
